﻿using System;
using System.Linq;
using System.Collections.Generic;

using Job.Core;
using Job.Core.Module;

namespace Job.ManageMember
{
    /// <summary>
    /// Role 类。
    /// </summary>
    [Serializable]
    public class Role : Business<Int32, Role>
    {

        #region Properties

        /// <summary>
        /// 角色名称。
        /// </summary>
        public String RoleName { get; set; }

        /// <summary>
        /// 系统角色。
        /// </summary>
        public Boolean IsSys { get; set; }

        ///// <summary>
        ///// 系统角色。 对应 SysTypeStatus
        ///// </summary>
        //public SysTypeStatus SysType { get; set; }

        /// <summary>
        /// 描述。
        /// </summary>
        public String Description { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// 获取所有 Role 列表。
        /// </summary>
        public static IEnumerable<Role> GetRoles()
        {
            return MemberService.Repository.GetRoles();
        }

        #endregion

        #region Overrides

        protected override Role DataLoad(Int32 id)
        {
            return MemberService.Repository.GetRole(id);
        }

        protected override bool DataUpdate()
        {
            return MemberService.Repository.UpdateRole(this);
        }

        protected override bool DataCreate()
        {
            return MemberService.Repository.CreateRole(this);
        }

        protected override bool DataDelete()
        {
            return MemberService.Repository.DeleteRole(this);
        }

        protected override SearchResult<Role> DataSearch(SearchQuery Query)
        {
            return new SearchResult<Role>();
        }

        /// <summary>
        /// 有效性验证。
        /// </summary>
        protected override void ValidationRules(DataAction action)
        {
            base.ValidationRules(action);
            if (action == DataAction.Delete)
            {
                AddRule(IsSys, new RuleViolation("IsSys", "系统角色不能被删除。"));
                return;
            }

            int i = 0;
            IEnumerable<Role> list = Role.GetRoles();
            foreach (var item in list)
            {
                if (RoleName != item.RoleName)
                    continue;

                if (action == DataAction.Update && KeyId != item.KeyId)
                    i++;
                if (action == DataAction.Create)
                    i++;
            }
            if (action == DataAction.Create || action == DataAction.Update)
            {
                AddRule(i > 0, new RuleViolation("RoleName", "角色名称被使用了。"));
                return;
            }

            AddRule(string.IsNullOrEmpty(RoleName), new RuleViolation("RoleName", "角色名称为必填项。"));
        }

        public override string ToString()
        {
            return RoleName;
        }

        #endregion

    }


    //public enum SysTypeStatus
    //{
    //    系统 = 1,
    //    财务 = 2,
    //    仓库人员 = 3,
    //    下单人员 = 4,
    //}
}
